home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / ScoreEffectWindow.c < prev    next >
Text File  |  1995-01-05  |  16KB  |  546 lines

  1. /* ScoreEffectWindow.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "ScoreEffectWindow.h"
  31. #include "MainWindowStuff.h"
  32. #include "WindowDispatcher.h"
  33. #include "TextEdit.h"
  34. #include "Memory.h"
  35. #include "GlobalWindowMenuList.h"
  36. #include "Main.h"
  37. #include "GrowIcon.h"
  38. #include "FindDialog.h"
  39. #include "DataMunging.h"
  40. #include "EffectSpecList.h"
  41.  
  42.  
  43. #define WINDOWLEFT (15)
  44. #define WINDOWTOP (30)
  45. #define WINDOWWIDTH (480)
  46. #define WINDOWHEIGHT (250)
  47.  
  48.  
  49. struct ScoreEffectWindowRec
  50.     {
  51.         MainWindowRec*            MainWindow;
  52.         WinType*                        ScreenID;
  53.         GenericWindowRec*        MyGenericWindow; /* how the window event dispatcher knows us */
  54.         MenuItemType*                MyMenuItem;
  55.         TextEditRec*                Editor;
  56.     };
  57.  
  58.  
  59. /* create a new score effect window. */
  60. ScoreEffectWindowRec*    NewScoreEffectWindow(struct MainWindowRec* MainWindow,
  61.                                                 char* Text)
  62.     {
  63.         ScoreEffectWindowRec*        Window;
  64.  
  65.         CheckPtrExistence(MainWindow);
  66.  
  67.         Window = (ScoreEffectWindowRec*)AllocPtrCanFail(sizeof(ScoreEffectWindowRec),
  68.             "ScoreEffectWindowRec");
  69.         if (Window == NIL)
  70.             {
  71.              FailurePoint1:
  72.                 return NIL;
  73.             }
  74.  
  75.         Window->MainWindow = MainWindow;
  76.  
  77.         Window->ScreenID = MakeNewWindow(eDocumentWindow,eWindowClosable,
  78.             eWindowZoomable,eWindowResizable,WINDOWLEFT,WINDOWTOP,WINDOWWIDTH,WINDOWHEIGHT,
  79.             (void (*)(void*))&ScoreEffectWindowUpdator,Window);
  80.         if (Window->ScreenID == 0)
  81.             {
  82.              FailurePoint2:
  83.                 ReleasePtr((char*)Window);
  84.                 goto FailurePoint1;
  85.             }
  86.  
  87.         Window->Editor = NewTextEdit(Window->ScreenID,
  88.             (TEScrollType)(eTEVScrollBar | eTEHScrollBar),GetMonospacedFont(),9,
  89.             -1,-1,GetWindowWidth(Window->ScreenID) + 2,GetWindowHeight(Window->ScreenID) + 2);
  90.         if (Window->Editor == NIL)
  91.             {
  92.              FailurePoint3:
  93.                 KillWindow(Window->ScreenID);
  94.                 goto FailurePoint2;
  95.             }
  96.  
  97.         Window->MyGenericWindow = CheckInNewWindow(Window->ScreenID,Window,
  98.             (void (*)(void*,MyBoolean,OrdType,OrdType,ModifierFlags))&ScoreEffectWindowDoIdle,
  99.             (void (*)(void*))&ScoreEffectWindowBecomeActive,
  100.             (void (*)(void*))&ScoreEffectWindowBecomeInactive,
  101.             (void (*)(void*))&ScoreEffectWindowJustResized,
  102.             (void (*)(OrdType,OrdType,ModifierFlags,void*))&ScoreEffectWindowDoMouseDown,
  103.             (void (*)(unsigned char,ModifierFlags,void*))&ScoreEffectWindowDoKeyDown,
  104.             (void (*)(void*))&ScoreEffectWindowClose,
  105.             (void (*)(void*))&ScoreEffectWindowMenuSetup,
  106.             (void (*)(void*,MenuItemType*))&ScoreEffectWindowDoMenuCommand);
  107.         if (Window->MyGenericWindow == NIL)
  108.             {
  109.              FailurePoint4:
  110.                 DisposeTextEdit(Window->Editor);
  111.                 goto FailurePoint3;
  112.             }
  113.  
  114.         Window->MyMenuItem = MakeNewMenuItem(mmWindowMenu,"x",0);
  115.         if (Window->MyMenuItem == NIL)
  116.             {
  117.              FailurePoint5:
  118.                 CheckOutDyingWindow(Window->MyGenericWindow);
  119.                 goto FailurePoint4;
  120.             }
  121.  
  122.         if (!RegisterWindowMenuItem(Window->MyMenuItem,(void (*)(void*))&ActivateThisWindow,
  123.             Window->ScreenID))
  124.             {
  125.              FailurePoint6:
  126.                 KillMenuItem(Window->MyMenuItem);
  127.                 goto FailurePoint5;
  128.             }
  129.  
  130.         SetTextEditAutoIndent(Window->Editor,True);
  131.         SetTextEditTabSize(Window->Editor,MainWindowGetTabSize(MainWindow));
  132.  
  133.         TextEditNewRawData(Window->Editor,Text,"\x0a");
  134.         TextEditHasBeenSaved(Window->Editor);
  135.  
  136.         ScoreEffectWindowResetTitlebar(Window);
  137.  
  138.         return Window;
  139.     }
  140.  
  141.  
  142. /* dispose of a score effect window. */
  143. void                                    DisposeScoreEffectWindow(ScoreEffectWindowRec* Window)
  144.     {
  145.         CheckPtrExistence(Window);
  146.  
  147.         /* save data */
  148.         if (!ScoreEffectWindowWritebackModifiedData(Window))
  149.             {
  150.                 /* failed -- now what? */
  151.             }
  152.  
  153.         MainWindowNotifyScoreEffectWindowClosed(Window->MainWindow,Window);
  154.         DeregisterWindowMenuItem(Window->MyMenuItem);
  155.         KillMenuItem(Window->MyMenuItem);
  156.         CheckOutDyingWindow(Window->MyGenericWindow);
  157.         DisposeTextEdit(Window->Editor);
  158.         KillWindow(Window->ScreenID);
  159.         ReleasePtr((char*)Window);
  160.     }
  161.  
  162.  
  163. void                                    ScoreEffectWindowDoIdle(ScoreEffectWindowRec* Window,
  164.                                                 MyBoolean CheckCursorFlag, OrdType XLoc, OrdType YLoc,
  165.                                                 ModifierFlags Modifiers)
  166.     {
  167.         CheckPtrExistence(Window);
  168.         TextEditUpdateCursor(Window->Editor);
  169.         if (CheckCursorFlag)
  170.             {
  171.                 if (TextEditIBeamTest(Window->Editor,XLoc,YLoc))
  172.                     {
  173.                         SetIBeamCursor();
  174.                     }
  175.                  else
  176.                     {
  177.                         SetArrowCursor();
  178.                     }
  179.             }
  180.     }
  181.  
  182.  
  183. void                                    ScoreEffectWindowBecomeActive(ScoreEffectWindowRec* Window)
  184.     {
  185.         OrdType                        XSize;
  186.         OrdType                        YSize;
  187.  
  188.         CheckPtrExistence(Window);
  189.         EnableTextEditSelection(Window->Editor);
  190.         XSize = GetWindowWidth(Window->ScreenID);
  191.         YSize = GetWindowHeight(Window->ScreenID);
  192.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  193.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(True/*enablegrowicon*/));
  194.     }
  195.  
  196.  
  197. void                                    ScoreEffectWindowBecomeInactive(ScoreEffectWindowRec* Window)
  198.     {
  199.         OrdType                        XSize;
  200.         OrdType                        YSize;
  201.  
  202.         CheckPtrExistence(Window);
  203.         DisableTextEditSelection(Window->Editor);
  204.         XSize = GetWindowWidth(Window->ScreenID);
  205.         YSize = GetWindowHeight(Window->ScreenID);
  206.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  207.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(False/*disablegrowicon*/));
  208.     }
  209.  
  210.  
  211. void                                    ScoreEffectWindowJustResized(ScoreEffectWindowRec* Window)
  212.     {
  213.         OrdType                        XSize;
  214.         OrdType                        YSize;
  215.  
  216.         CheckPtrExistence(Window);
  217.         XSize = GetWindowWidth(Window->ScreenID);
  218.         YSize = GetWindowHeight(Window->ScreenID);
  219.         SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  220.         DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  221.         SetTextEditPosition(Window->Editor,-1,-1,XSize + 2,YSize + 2);
  222.     }
  223.  
  224.  
  225. void                                    ScoreEffectWindowDoMouseDown(OrdType XLoc, OrdType YLoc,
  226.                                                 ModifierFlags Modifiers, ScoreEffectWindowRec* Window)
  227.     {
  228.         CheckPtrExistence(Window);
  229.         if ((XLoc >= GetWindowWidth(Window->ScreenID) - 15)
  230.             && (XLoc < GetWindowWidth(Window->ScreenID))
  231.             && (YLoc >= GetWindowHeight(Window->ScreenID) - 15)
  232.             && (YLoc < GetWindowHeight(Window->ScreenID)))
  233.             {
  234.                 UserGrowWindow(Window->ScreenID,XLoc,YLoc);
  235.                 ScoreEffectWindowJustResized(Window);
  236.             }
  237.         else if (TextEditHitTest(Window->Editor,XLoc,YLoc))
  238.             {
  239.                 TextEditDoMouseDown(Window->Editor,XLoc,YLoc,Modifiers);
  240.             }
  241.     }
  242.  
  243.  
  244. void                                    ScoreEffectWindowDoKeyDown(unsigned char KeyCode,
  245.                                                 ModifierFlags Modifiers, ScoreEffectWindowRec* Window)
  246.     {
  247.         CheckPtrExistence(Window);
  248.         TextEditDoKeyPressed(Window->Editor,KeyCode,Modifiers);
  249.     }
  250.  
  251.  
  252. void                                    ScoreEffectWindowClose(ScoreEffectWindowRec* Window)
  253.     {
  254.         CheckPtrExistence(Window);
  255.         DisposeScoreEffectWindow(Window);
  256.     }
  257.  
  258.  
  259. void                                    ScoreEffectWindowUpdator(ScoreEffectWindowRec* Window)
  260.     {
  261.         OrdType            XSize;
  262.         OrdType            YSize;
  263.  
  264.         CheckPtrExistence(Window);
  265.         TextEditFullRedraw(Window->Editor);
  266.         XSize = GetWindowWidth(Window->ScreenID);
  267.         YSize = GetWindowHeight(Window->ScreenID);
  268.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  269.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,
  270.             GetGrowIcon(Window->MyGenericWindow == GetCurrentWindowID()));
  271.     }
  272.  
  273.  
  274. void                                    ScoreEffectWindowMenuSetup(ScoreEffectWindowRec* Window)
  275.     {
  276.         CheckPtrExistence(Window);
  277.         MainWindowEnableGlobalMenus(Window->MainWindow);
  278.         EnableMenuItem(mPaste);
  279.         ChangeItemName(mPaste,"Paste Text");
  280.         if (TextEditIsThereValidSelection(Window->Editor))
  281.             {
  282.                 EnableMenuItem(mCut);
  283.                 ChangeItemName(mCut,"Cut Text");
  284.                 EnableMenuItem(mCopy);
  285.                 ChangeItemName(mCopy,"Copy Text");
  286.                 EnableMenuItem(mClear);
  287.                 ChangeItemName(mClear,"Clear Text");
  288.             }
  289.         EnableMenuItem(mShiftLeft);
  290.         EnableMenuItem(mShiftRight);
  291.         EnableMenuItem(mBalanceParens);
  292.         EnableMenuItem(mSelectAll);
  293.         ChangeItemName(mSelectAll,"Select All Text");
  294.         if (TextEditCanWeUndo(Window->Editor))
  295.             {
  296.                 EnableMenuItem(mUndo);
  297.                 ChangeItemName(mUndo,"Undo Text Change");
  298.             }
  299.         ChangeItemName(mCloseFile,"Close Score Effects");
  300.         EnableMenuItem(mCloseFile);
  301.         EnableMenuItem(mFind);
  302.         if (PtrSize(GlobalSearchString) != 0)
  303.             {
  304.                 EnableMenuItem(mFindAgain);
  305.                 if (TextEditIsThereValidSelection(Window->Editor))
  306.                     {
  307.                         EnableMenuItem(mReplace);
  308.                         EnableMenuItem(mReplaceAndFindAgain);
  309.                     }
  310.             }
  311.         EnableMenuItem(mShowSelection);
  312.         if (TextEditIsThereValidSelection(Window->Editor))
  313.             {
  314.                 EnableMenuItem(mEnterSelection);
  315.             }
  316.         SetItemCheckmark(Window->MyMenuItem);
  317.         ChangeItemName(mBuildFunction,"Compile Score Effects");
  318.         EnableMenuItem(mBuildFunction);
  319.     }
  320.  
  321.  
  322. void                                    ScoreEffectWindowDoMenuCommand(ScoreEffectWindowRec* Window,
  323.                                                 MenuItemType* MenuItem)
  324.     {
  325.         CheckPtrExistence(Window);
  326.         if (MainWindowDoGlobalMenuItem(Window->MainWindow,MenuItem))
  327.             {
  328.             }
  329.         else if (MenuItem == mPaste)
  330.             {
  331.                 TextEditDoMenuPaste(Window->Editor);
  332.             }
  333.         else if (MenuItem == mCut)
  334.             {
  335.                 TextEditDoMenuCut(Window->Editor);
  336.             }
  337.         else if (MenuItem == mCopy)
  338.             {
  339.                 TextEditDoMenuCopy(Window->Editor);
  340.             }
  341.         else if (MenuItem == mClear)
  342.             {
  343.                 TextEditDoMenuClear(Window->Editor);
  344.             }
  345.         else if (MenuItem == mSelectAll)
  346.             {
  347.                 TextEditDoMenuSelectAll(Window->Editor);
  348.             }
  349.         else if (MenuItem == mUndo)
  350.             {
  351.                 TextEditDoMenuUndo(Window->Editor);
  352.             }
  353.         else if (MenuItem == mCloseFile)
  354.             {
  355.                 ScoreEffectWindowClose(Window);
  356.             }
  357.         else if (MenuItem == mShiftLeft)
  358.             {
  359.                 TextEditShiftSelectionLeftOneTab(Window->Editor);
  360.             }
  361.         else if (MenuItem == mShiftRight)
  362.             {
  363.                 TextEditShiftSelectionRightOneTab(Window->Editor);
  364.             }
  365.         else if (MenuItem == mBalanceParens)
  366.             {
  367.                 TextEditBalanceParens(Window->Editor);
  368.             }
  369.         else if (MenuItem == mFind)
  370.             {
  371.                 switch (DoFindDialog(&GlobalSearchString,&GlobalReplaceString,
  372.                     mCut,mPaste,mCopy,mUndo,mSelectAll,mClear))
  373.                     {
  374.                         default:
  375.                             EXECUTE(PRERR(ForceAbort,
  376.                                 "ScoreEffectWindowDoMenuCommand:  bad value from DoFindDialog"));
  377.                             break;
  378.                         case eFindCancel:
  379.                         case eDontFind:
  380.                             break;
  381.                         case eFindFromStart:
  382.                             SetTextEditInsertionPoint(Window->Editor,0,0);
  383.                             TextEditFindAgain(Window->Editor,GlobalSearchString);
  384.                             TextEditShowSelection(Window->Editor);
  385.                             break;
  386.                         case eFindAgain:
  387.                             TextEditFindAgain(Window->Editor,GlobalSearchString);
  388.                             TextEditShowSelection(Window->Editor);
  389.                             break;
  390.                     }
  391.             }
  392.         else if (MenuItem == mFindAgain)
  393.             {
  394.                 TextEditFindAgain(Window->Editor,GlobalSearchString);
  395.                 TextEditShowSelection(Window->Editor);
  396.             }
  397.         else if (MenuItem == mReplace)
  398.             {
  399.                 if (TextEditIsThereValidSelection(Window->Editor))
  400.                     {
  401.                         TextEditInsertRawDataWithUndo(Window->Editor,GlobalReplaceString,
  402.                             SYSTEMLINEFEED);
  403.                     }
  404.             }
  405.         else if (MenuItem == mReplaceAndFindAgain)
  406.             {
  407.                 if (TextEditIsThereValidSelection(Window->Editor))
  408.                     {
  409.                         TextEditInsertRawDataWithUndo(Window->Editor,GlobalReplaceString,
  410.                             SYSTEMLINEFEED);
  411.                         TextEditFindAgain(Window->Editor,GlobalSearchString);
  412.                         TextEditShowSelection(Window->Editor);
  413.                     }
  414.             }
  415.         else if (MenuItem == mShowSelection)
  416.             {
  417.                 TextEditShowSelection(Window->Editor);
  418.             }
  419.         else if (MenuItem == mEnterSelection)
  420.             {
  421.                 char*                        NewString;
  422.  
  423.                 NewString = TextEditGetSelection(Window->Editor);
  424.                 if (NewString != NIL)
  425.                     {
  426.                         ReleasePtr(GlobalSearchString);
  427.                         GlobalSearchString = NewString;
  428.                     }
  429.             }
  430.         else if (MenuItem == mBuildFunction)
  431.             {
  432.                 EffectSpecListRec*        EffectSpec;
  433.  
  434.                 EffectSpec = MainWindowGetScoreEffectsSpec(Window->MainWindow);
  435.                 if (EffectSpec != NIL)
  436.                     {
  437.                         DisposeEffectSpecList(EffectSpec);
  438.                     }
  439.             }
  440.         else
  441.             {
  442.                 EXECUTE(PRERR(AllowResume,"ScoreEffectWindowDoMenuCommand:  unknown menu command"));
  443.             }
  444.     }
  445.  
  446.  
  447. /* get the string from the editor */
  448. char*                                    ScoreEffectWindowGetText(ScoreEffectWindowRec* Window)
  449.     {
  450.         CheckPtrExistence(Window);
  451.         return TextEditGetRawData(Window->Editor,"\x0a");
  452.     }
  453.  
  454.  
  455. /* make editor window come to top */
  456. void                                    ScoreEffectWindowBringToTop(ScoreEffectWindowRec* Window)
  457.     {
  458.         CheckPtrExistence(Window);
  459.         ActivateThisWindow(Window->ScreenID);
  460.     }
  461.  
  462.  
  463. /* utility routine to hilite a text line on which a compile error has occurred */
  464. void                                    ScoreEffectWindowHiliteLine(ScoreEffectWindowRec* Window,
  465.                                                 long ErrorLine)
  466.     {
  467.         CheckPtrExistence(Window);
  468.         SetTextEditSelection(Window->Editor,ErrorLine,0,ErrorLine + 1,0);
  469.         TextEditShowSelection(Window->Editor);
  470.     }
  471.  
  472.  
  473. /* the name of the document has changed, so change the name of the window */
  474. void                                    ScoreEffectWindowGlobalNameChange(ScoreEffectWindowRec* Window,
  475.                                                 char* NewFilename)
  476.     {
  477.         char*                            SeparatorString;
  478.  
  479.         CheckPtrExistence(Window);
  480.         CheckPtrExistence(NewFilename);
  481.  
  482.         SeparatorString = StringToBlockCopy(":  Score Effects");
  483.         if (SeparatorString != NIL)
  484.             {
  485.                 char*                            TotalString;
  486.  
  487.                 TotalString = ConcatBlockCopy(NewFilename,SeparatorString);
  488.                 if (TotalString != NIL)
  489.                     {
  490.                         char*                            NullTerminatedString;
  491.  
  492.                         NullTerminatedString = BlockToStringCopy(TotalString);
  493.                         if (NullTerminatedString != NIL)
  494.                             {
  495.                                 SetWindowName(Window->ScreenID,NullTerminatedString);
  496.                                 ChangeItemName(Window->MyMenuItem,NullTerminatedString);
  497.                                 ReleasePtr(NullTerminatedString);
  498.                             }
  499.                         ReleasePtr(TotalString);
  500.                     }
  501.                 ReleasePtr(SeparatorString);
  502.             }
  503.     }
  504.  
  505.  
  506. /* refresh the titlebar of the window */
  507. void                                    ScoreEffectWindowResetTitlebar(ScoreEffectWindowRec* Window)
  508.     {
  509.         char*                            DocumentName;
  510.  
  511.         CheckPtrExistence(Window);
  512.         DocumentName = GetCopyOfDocumentName(Window->MainWindow);
  513.         if (DocumentName != NIL)
  514.             {
  515.                 ScoreEffectWindowGlobalNameChange(Window,DocumentName);
  516.                 ReleasePtr(DocumentName);
  517.             }
  518.     }
  519.  
  520.  
  521. /* force the window to write back to the object any data that has changed */
  522. MyBoolean                            ScoreEffectWindowWritebackModifiedData(ScoreEffectWindowRec* Window)
  523.     {
  524.         MyBoolean                    SuccessFlag = True;
  525.  
  526.         CheckPtrExistence(Window);
  527.  
  528.         if (TextEditDoesItNeedToBeSaved(Window->Editor))
  529.             {
  530.                 char*                    String;
  531.  
  532.                 String = ScoreEffectWindowGetText(Window);
  533.                 if (String != NIL)
  534.                     {
  535.                         PutMainWindowScoreEffects(Window->MainWindow,String);
  536.                         TextEditHasBeenSaved(Window->Editor);
  537.                     }
  538.                  else
  539.                     {
  540.                         SuccessFlag = False;
  541.                     }
  542.             }
  543.  
  544.         return SuccessFlag;
  545.     }
  546.